package com.campus.dao.impl;

import com.campus.dao.TeacherDao;
import com.campus.dao.UserDao;
import com.campus.model.Teacher;
import com.campus.model.User;
import com.mysql.cj.jdbc.ConnectionImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.sql.*;
import java.util.List;

@Repository
@Slf4j
public class TeacherDaoImpl implements TeacherDao {
    @Autowired
    private Connection conn;

    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Autowired
    private UserDao userDao;
    @Override
    public Integer addTeacher(Teacher teacher) {
        User user = new User();
        user.setName(teacher.getName());
        user.setPassword("123456");
        user.setUser_id(teacher.getLogin_id());
        userDao.insert(user);

        String sql=" INSERT INTO t_manager_teacher (teacher_name,subject,profession,school,introduce,login_id) VALUES " +
                " (?,?,?,?,?,?)";
        Object[] args = new Object[]{
                teacher.getName(),
                teacher.getSubject(),
                teacher.getProfession(),
                teacher.getSchool(),
                teacher.getIntroduce(),
                teacher.getLogin_id(),
        };
        return jdbcTemplate.update(sql,args);


    }

    @Override
    public Teacher queryTeacherById(int teacherId) {
        String sql=" SELECT * FROM t_manager_teacher WHERE id=?";
        try (PreparedStatement ps = this.conn.prepareStatement(sql) ;
        ) {
            ps.setInt(1,teacherId); ;
            ResultSet rs = ps.executeQuery() ;
            if (rs.next()) {
                Teacher teacher = new Teacher()
                        .setTeacher_id(teacherId)
                        .setName(rs.getString(2))
                        .setSubject(rs.getString(3))
                        .setProfession(rs.getString(4))
                        .setSchool(rs.getString(5))
                        .setIntroduce(rs.getString(6));
                return teacher ;
            }
        } catch (SQLException e) {
            log.error(e.getMessage()) ;
        }
        return null;
    }

    @Override
    public List<Teacher> queryTeacherByName(String name) {
        String sql = " SELECT * FROM t_manager_teacher WHERE teacher_name LIKE ?";
        return this.jdbcTemplate.query(sql, new Object[]{"%" + name + "%"}, new RowMapper<Teacher>() {
            public Teacher mapRow(ResultSet rs, int rowNum) throws SQLException {
                Teacher teacher = new Teacher();
                teacher.setTeacher_id(rs.getInt("id"));
                teacher.setName(rs.getString("teacher_name"));
                teacher.setSubject(rs.getString("subject"));
                teacher.setProfession(rs.getString("profession"));
                teacher.setSchool(rs.getString("school"));
                teacher.setIntroduce(rs.getString("introduce"));
                return teacher;
            }
        });
    }
}
